package com.p7.framework.common.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

import com.p7.framework.common.exception.RequestParamException;

public class DateConverter implements Converter<String, Date> {

	private String datePattern;

	public DateConverter(String datePattern) {
		this.datePattern = datePattern;
	}

	@Override
	public Date convert(String source) {

		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
			dateFormat.setLenient(false);
			return dateFormat.parse(source);
		} catch (ParseException e) {
			/**
			 * 不能直接将错误消息转到前端
			 */
			throw new RequestParamException("日期格式错误");
		}

	}

}
